package lacus.wd_cinema.controller.views;

import lacus.wd_cinema.service.LogService;
import lacus.wd_cinema.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 管理员页面接口控制类
 * @author lacus
 * @version 0.4
 * @since 0.4
 */
@Controller
@RequestMapping("/admin")
public class AdminViewController {
    public final UserService userService;
    private final LogService logService;

    public AdminViewController(UserService userService, LogService logService) {
        this.userService = userService;
        this.logService = logService;
    }

    @GetMapping
    public String getHomePage() {
        return "redirect:/admin/users";
    }

    @GetMapping("/users")
    public String getUserListPage(
            Model model,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "") String search) {
        model.addAttribute("page",
                userService.getAllUsers(search, keyword, page - 1, size));
        model.addAttribute("keyword", keyword);
        model.addAttribute("search", search);
        return "admin/UserListPage";
    }

    @GetMapping("/users/create")
    public String getUserCreatePage() {
        return "admin/UserCreatePage";
    }

    @GetMapping("/users/{id}")
    public String getUserPage(
            @PathVariable int id, Model model) {
        model.addAttribute("user", userService.getUserById(id));
        return "admin/UserDetailsPage";
    }

    @GetMapping("/users/{id}/edit")
    public String getUserEditPage(
            @PathVariable int id, Model model) {
        model.addAttribute("user", userService.getUserById(id));
        return "admin/UserEditPage";
    }

    @GetMapping("/log")
    public String getAdminLogPage(Model model) {
        model.addAttribute("log", logService.getAdminLog());
        return "admin/AdminLogPage";
    }
}
